package com.UKIT.airtickets.DB

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [User::class, UserSettings::class, UserFavorite::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
    abstract fun userSettingsDao(): UserSettingsDao
    abstract fun userFavoriteDao(): UserFavoriteDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "airtickets.db"
                ).build()
                INSTANCE = instance
                return instance
            }
        }
    }
}

class DatabaseManager(context: Context) {
    private val db: AppDatabase = AppDatabase.getDatabase(context)

    fun userDao(): UserDao {
        return db.userDao()
    }

    fun userSettingsDao(): UserSettingsDao {
        return db.userSettingsDao()
    }

    fun userFavoriteDao(): UserFavoriteDao {
        return db.userFavoriteDao()
    }
}

